package cn.jietuo.second.class01;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/10/2
 * @description: 在一个有序数组中，找某个数是否存在
 * 直接通过二分查找来确定这个数是否存在
 */
public class Code04_BSExist {

    public static boolean bsExist(int[] array,int target){
        if (array == null || array.length < 0){
            return false;
        }
        int left = 0;
        int right = array.length -1;
        int mid = -1;
        while(left <= right){
            mid = left + ((right - left)>> 1);
            // 先判断中间值与target的大小
            if (array[mid] > target){
                right = mid -1;
            }else if(array[mid] < target){
                left = mid + 1;
            }else{
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9};
        System.out.println(bsExist(array, 1));
    }
}
